package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.domain.*;
import com.interview.one.model.MessageInfo;
import com.interview.one.model.Order;
import com.interview.one.model.OrderGoods;
import com.interview.one.model.OrderInfo;
import com.interview.one.service.*;
import com.interview.one.utils.DateUtil;
import com.interview.one.utils.YuanFenConverter;
import com.interview.one.utils.push.PushSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ShipAddressService shipAddressService;

    @Autowired
    private CartGoodsService cartGoodsService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @PostMapping("/getOrderList")
    public CommonRes getOrderList(@RequestBody GetOrderListReq req, @RequestHeader String token) throws BusinessException {
        List<OrderInfo> list = this.orderService.getOrderList(Integer.valueOf(token), req.getOrderStatus());
        if ((list == null) || (list.size() == 0)) {
            throw new BusinessException(EmBusinessError.NO_DATA_ERROR);
        }

        List orderList = new ArrayList();
        for (OrderInfo info : list) {
            Order order = new Order();
            order.setId(info.getId());
            order.setOrderStatus(info.getOrderStatus());
            order.setPayType(info.getPayType());
            order.setTotalPrice(YuanFenConverter.changeY2F(Long.valueOf(info.getTotalPrice())));
            order.setShipAddress(this.shipAddressService.getShipAddressById(info.getShipId()));

            List<OrderGoods> goodsList = this.orderService.getOrderGoodsList(info.getId());
            for (OrderGoods orderGoods : goodsList) {
                orderGoods.setGoodsPrice(YuanFenConverter.changeY2F(orderGoods.getGoodsPrice()));
            }

            order.setOrderGoodsList(goodsList);

            orderList.add(order);
        }
        return CommonRes.create(list);
    }

    @PostMapping("/getOrderById")
    public CommonRes  getOrderById(@RequestBody GetOrderByIdReq req){
        Order order = new Order();

        OrderInfo orderInfo = this.orderService.getOrderById(req.getOrderId());
        order.setId(orderInfo.getId());
        order.setOrderStatus(orderInfo.getOrderStatus());
        order.setPayType(orderInfo.getPayType());
        order.setTotalPrice(YuanFenConverter.changeY2F(Long.valueOf(orderInfo.getTotalPrice())));

        order.setShipAddress(this.shipAddressService.getShipAddressById(orderInfo.getShipId()));

        List<OrderGoods> orderGoodsList = this.orderService.getOrderGoodsList(orderInfo.getId());
        for (OrderGoods orderGoods : orderGoodsList) {
            orderGoods.setGoodsPrice(YuanFenConverter.changeY2F(orderGoods.getGoodsPrice()));
        }

        order.setOrderGoodsList(orderGoodsList);

        return CommonRes.create(order);
    }

    @PostMapping("/submitOrder")
    public CommonRes submitOrder(@RequestBody SubmitOrderReq req, @RequestHeader String token) {
        OrderInfo orderInfo = this.orderService.getOrderById(req.getOrder().getId());
        orderInfo.setOrderStatus(1);
        orderInfo.setShipId(req.getOrder().getShipAddress().getId());

        this.orderService.modifyOrder(orderInfo);

        List cartIdList = (List) InitAction.cartIdMap.get(req.getOrder().getId());
        this.cartGoodsService.deleteCartGoods(cartIdList);
        InitAction.cartIdMap.remove(req.getOrder().getId());

        int userId = Integer.parseInt(token);
        String pushId = this.userService.getUserById(userId).getPushId();
        // sendMessage(Integer.valueOf(userId), pushId, req.getOrder().getId() + "");
        return CommonRes.create("订单提交成功");
    }

    private void sendMessage(Integer userId, String pushId, String orderId) {
        String curTime = DateUtil.format(new Date(), DateUtil.FORMAT_LONG_NEW);
        MessageInfo msg = new MessageInfo();
        msg.setMsgIcon("http://osea2fxp7.bkt.clouddn.com/108x108.png");
        msg.setMsgTitle("下单成功");
        msg.setMsgContent("恭喜您下单成功，有一笔订单等待支付");
        msg.setMsgTime(curTime);
        msg.setUserId(userId);
        this.messageService.addMessage(msg);

        PushSender.sendOrderEvent(pushId, orderId);
    }

    @PostMapping("/cancel")
    public CommonRes cancelOrder(@RequestBody CancelOrderReq req) {
        OrderInfo orderInfo = this.orderService.getOrderById(req.getOrderId());
        orderInfo.setOrderStatus(4);
        this.orderService.modifyOrder(orderInfo);
        return CommonRes.create("订单取消成功");
    }

    @PostMapping("/confirm")
    public CommonRes confirmOrder(@RequestBody ConfirmOrderReq req) {
        OrderInfo orderInfo = this.orderService.getOrderById(req.getOrderId());
        orderInfo.setOrderStatus(3);
        this.orderService.modifyOrder(orderInfo);

        return CommonRes.create("订单确认收货成功");
    }

    @PostMapping("/pay")
    public CommonRes payOrder(@RequestBody PayOrderReq req) {
        OrderInfo orderInfo = this.orderService.getOrderById(req.getOrderId());
        orderInfo.setOrderStatus(2);
        this.orderService.modifyOrder(orderInfo);
        return CommonRes.create("订单支付成功");
    }
}
